﻿using System;
using System.IO;

namespace ooWii.IO
{
	public class ReopenStream : Stream
	{
		#region ReopenStream

		Stream stream;
		long position;

		public ReopenStream( Stream stream )
		{
			this.stream = stream;
		}

		#endregion

		#region Stream

		public override bool CanRead
		{
			get
			{
				return stream.CanRead;
			}
		}

		public override bool CanSeek
		{
			get
			{
				return stream.CanSeek;
			}
		}

		public override bool CanWrite
		{
			get
			{
				return stream.CanWrite;
			}
		}

		public override void Flush()
		{
			stream.Flush();
		}

		public override long Length
		{
			get
			{
				return stream.Length;
			}
		}

		public override long Position
		{
			get
			{
				return position;
			}
			set
			{
				position = value;
			}
		}

		public override int Read( byte[] buffer, int offset, int count )
		{
			stream.Position = position;
			int bytes = stream.Read( buffer, offset, count );
			position += bytes;
			return bytes;
		}

		public override long Seek( long offset, SeekOrigin origin )
		{
			position = stream.Seek( offset, origin );
			return position;
		}

		public override void SetLength( long value )
		{
			stream.SetLength( value );
		}

		public override void Write( byte[] buffer, int offset, int count )
		{
			stream.Position = position;
			stream.Write( buffer, offset, count );
			position += count;
		}

		public override void Close()
		{
			stream = null;
			base.Close();
		}

		#endregion
	}
}

